package pack1022;

//String
public class DemoA {

    private static void a() {
        String s = "abc";
        System.out.println(s);
        s = "bbb";
        System.out.println(s);
        int a = 1;
        System.out.println(a);
        a = 2;
        System.out.println(a);
    }

    //如何产生这个类的对象
    private static void b() {
        //产生了一个空字符串“”
        String str = new String();
        //产生了一个内容为abc的字符串
        String str1 = new String("abc");
        //产生了一个内容为abc的字符串 最常用
        String str2 = "abc";
        //以char[]产生一个字符串
        char[] chars = {'a', 'b', 'c', 'd', 'e'};
        String str3 = new String(chars);
        String str4 = new String(chars, 0, 4);
        //以byte[]产生一个字符串
        byte[] bytes = {1, 2, 3, 4, 5, 6};
        String str5 = new String(bytes);
        String str6 = new String(bytes, 0, 2);
        System.out.println(str);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);
        System.out.println(str5);
        System.out.println(str6);
    }

    //这个类的常见方法有哪些
    private static void c1() {
        String str = "auhdfwiahfgawfh8aw9fg09apwf8gh";
        //长度
        int len = str.length();
        //获取字符串中指定位置的字符
        char ch = str.charAt(4);
        //获取大写形式和小写形式
        String lowercase = str.toLowerCase();
        String toUpperCase = str.toUpperCase();
        //是否是空字符串”“
        boolean empty = str.isEmpty();
        //将字符串转换成字节数组和字符数组
        byte[] bytes = str.getBytes();
        char[] chars = str.toCharArray();
        System.out.println(len);
        System.out.println(ch);
        System.out.println(lowercase);
        System.out.println(toUpperCase);
        System.out.println(empty);
        System.out.println(bytes);
        System.out.println(chars);
    }

    private static void c2() {
        String str = "  uadha  ijdjadj  isa  ";
        //去除前后空格产生一个新的字符串
        String trim = str.trim();
        //替换字符串产生一个新的字符串
        String newStr = str.replace(" ", "");
        System.out.println(str);
        System.out.println(trim);
        System.out.println(newStr);
    }

    private static void c3() {
        String str = "ajigduagdawgdgaikg";
        //是否以某字符串开头
        boolean starts = str.startsWith("aj");
        //是否以某字符串结尾
        boolean ends = str.endsWith("kg");
        System.out.println(starts);
        System.out.println(ends);
    }

    private static void c4() {
        String str = "audfgbvhywiugfuagwiauwgh";
        //找出au在字符串中第一次出现的位置 不存在返回-1
        int index1 = str.indexOf("au");
        //找出au在字符串最后一次出现的位置 不存在返回-1
        int index2 = str.lastIndexOf("au");
        //找出au从字符串第五个位置开始第一次出现的位置，不存在返回-1
        int index3 = str.indexOf("au", 4);
        System.out.println(index1);
        System.out.println(index2);
        System.out.println(index3);
    }

    private static void c5() {
        String str1 = "augfaguwdajkoasf";
        String str2 = str1.substring(4);
        String str3 = str1.substring(4, 9);//(4,9)
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }

    private static void c6() {

    }

    //一些注意事项
    private static void d1() {
        //一个对象
        String str1 = "abc";
        String str2 = "abc";
        System.out.println(str1 == str2);
        //新建了两个对象
        String str3 = new String("abc");
        String str4 = new String("abc");
        System.out.println(str3 == str4);
    }

    private static void d2() {
        //几个对象；2个new,abc.
        String str = new String("abc");
    }

    public static void main(String[] args) {
        c5();
    }
}
